<?php

/**
 * Класс RL_Stream_String -- строка в роли потока.
 * 
 * @package RL
 * @author Lucky <lucky@jetstyle.ru> 
 * @version $Id: String.php 62 2007-09-05 08:27:21Z lucky $
 */
class RL_Stream_String extends RL_Stream
{
	public $string = NULL;
	public $offset = NULL;

	public function getString ()
	{
		return $this->string;
	} // end of function getString

	public function setString ($value)
	{
		$this->string = $value;
		return $this;
	} // end of function setString

	public function open ($mode)
	{
		$this->offset = 0;
		return (isset($this->string)) ? TRUE : FALSE;
	} // end of function open

	public function close ()
	{
		return TRUE;
	} // end of function open

	public function read ($size)
	{
		$res = substr($this->string, $this->offset, $this->offset + $size);
		$this->offset += strlen($res);
		return $res;
	} // end of function open

	public function seek ($offset, $whence)
	{
		switch ($whence) {
		case SEEK_SET:
			if ($offset < strlen($this->string) && $offset >= 0) {
				$this->offset = $offset;
				return TRUE;
			} else {
				return FALSE;
			}
			break;

		case SEEK_CUR:
			if ($offset >= 0) {
				$this->offset += $offset;
				return TRUE;
			} else {
				return FALSE;
			}
			break;

		case SEEK_END:
			if (strlen($$this->string) + $offset >= 0) {
				$this->offset = strlen($$this->string) + $offset;
				return TRUE;
			} else {
				return FALSE;
			}
			break;

		default:
			return FALSE;
		}	
	} // end of function seek

	public function passthru ()
	{
		echo substr($this->string, $this->offset);
	} // end of function passthru

	public function rewind ()
	{
		$this->offset = 0;
	} // end of function rewind

	public function eof ()
	{
		return $this->offset >= strlen($this->string);
	} // end of function eof
}

